package com.koron.system.configSettings.controller;

import com.koron.bean.base.Response;
import com.koron.common.core.business.configsettings.bean.ConfigType;
import com.koron.common.core.business.configsettings.service.ConfigTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 系统管理-配置分类管理{@code ConfigType}控制层
 *
 * <p>主要提供：增加、删除、修改、查询功能</p>
 *
 * @author 朱超
 * @since 2021-08-31
 */
@RestController
@RequestMapping("sys/ConfigType")
@Api(tags = "系统管理-配置分类接口")
public class ConfigTypeController {

    @Autowired
    private ConfigTypeService configTypeService;

    /**
     * 新增单条数据
     *
     * @param configType
     * @return Response
     * @see Response
     * @version 1.0
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增单条配置项")
    public Response checkBeforeInsert(@RequestBody @Valid ConfigType configType) {
        return ADOConnection.runTask(configTypeService, "checkBeforeInsert", Response.class, configType);
    }

    /**
     * 逻辑删除单条数据
     *
     * @param id 主键
     * @return Response对象
     * @see Response
     * @version 1.0
     */
    @DeleteMapping("delete")
    @ApiOperation(value = "根据id删除单条配置分类")
    public Response delete(@RequestParam("id") String id) {
        return ADOConnection.runTask(configTypeService, "checkBeforeDelete", Response.class, id);
    }

    /**
     * 修改数据
     *
     * @param configType
     * @return Response
     * @see Response
     * @version 1.0
     */
    @PutMapping("update")
    @ApiOperation(value = "修改单条配置分类")
    public Response update(@RequestBody @Valid ConfigType configType) {
        return ADOConnection.runTask(configTypeService, "checkBeforeUpdate", Response.class, configType);
    }

    /**
     * 根据传入的参数，查询对应数据，并分页
     *
     * @param classifyCode, 传空的时候查出所有的数据
     * @param code
     * @param name
     * @param page
     * @param pageSize
     * @return Response
     * @see Response
     * @version 1.0
     */
    @GetMapping("getListAndPaging")
    @ApiOperation(value = "根据传入的配置分类编码，分页返回相应的数据")
    public Response getListAndPaging(@RequestParam(value = "classifyCode", required = false) String classifyCode, @RequestParam(value = "code", required = false) String code,
                                     @RequestParam(value = "name", required = false) String name, @RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize) {
        return ADOConnection.runTask(configTypeService, "getListAndPaging", Response.class,classifyCode,code,name,page,pageSize);
    }

    @GetMapping("getByClassifyCodeAndCode")
    @ApiOperation(value = "根据传入的classifyCode和code，返回对应的数据")
    public Response test(@RequestParam("classifyCode") String classifyCode,@RequestParam("code") String code,SessionFactory factory) {
        ConfigType configType = configTypeService.getByClassifyCodeAndCode(factory, classifyCode, code);
        return Response.success(configType);
    }

    @GetMapping("/languagelist")
    @ApiOperation(value = "语言清单 无token")
    public Response languagelist() {
        ConfigType configtype = ADOConnection.runTask(configTypeService, "getByClassifyCodeAndCode", ConfigType.class, "common", "language_config");
        return Response.success(configtype);
    }
}
